package com.swsc.xapp.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.swsc.xapp.enums.NJSFConsts;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author chencl
 * @Date 2022/7/26 13:58
 * @Version 1.0
 * @Description 相机
 */
public class CameraUtils {
    public static String imagePath;
    /**
     * 打开系统相机
     *
     * @param context
     */
    public static void openCameraGetPhoto(Context context, int code) {
        // 加载路径图片路径
        Uri photoUri = getMediaFileUri(code, null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            takePhotoBiggerThan7(context, imagePath);
        } else {
            // 指定拍照意图
            Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 指定存储路径，这样就可以保存原图了
            openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            ((Activity) context).startActivityForResult(openCameraIntent, NJSFConsts.CODE_OPEN_CAMERA);
        }
    }
    public static void openCameraGetPhoto(Context context, int code, String bucket) {
        // 加载路径图片路径
        Uri photoUri = getMediaFileUri(code, bucket);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            takePhotoBiggerThan7(context, imagePath);
        } else {
            // 指定拍照意图
            Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 指定存储路径，这样就可以保存原图了
            openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            ((Activity) context).startActivityForResult(openCameraIntent, NJSFConsts.CODE_OPEN_CAMERA);
        }
    }
    /**
     * 设置拍照图片存储位置
     *
     * @param type
     * @return
     */
    public static Uri getMediaFileUri(int type, String bucket) {
        //删除历史图片
        String imgDir = ConstantUils.DIR_INFRARED.concat(TextUtils.isEmpty(bucket) ? "0000/0000/" : bucket);
//        deleteImg(imgDir);
        File mediaStorageDir = new File(imgDir);
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }
        //创建Media File
        String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        imagePath = mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg";
        File mediaFile;
        if (type == NJSFConsts.CODE_OPEN_CAMERA) {
            mediaFile = new File(imagePath);
        } else {
            return null;
        }
        return Uri.fromFile(mediaFile);
    }

    //删除历史图片
    public static void deleteImg(String path) {
        String folderPath = path;
        File dir = new File(folderPath);
        if (dir == null || !dir.exists() || !dir.isDirectory() || dir.listFiles() == null)
            return;
        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete();
        }
    }

    /**
     * Android 7.0 及以上打开相机设置
     *
     * @param context
     * @param absolutePath
     */
    private static void takePhotoBiggerThan7(Context context, String absolutePath) {
        Uri mCameraTempUri;
        try {
            ContentValues values = new ContentValues(1);
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
            values.put(MediaStore.Images.Media.DATA, absolutePath);
            mCameraTempUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            if (mCameraTempUri != null) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraTempUri);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            }
            ((Activity) context).startActivityForResult(intent, NJSFConsts.CODE_OPEN_CAMERA);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
